//
//  EventViewController.m
//  sy_pad_l
//
//  Created by Chiu Chi Ho on 4/2/13.
//  Copyright (c) 2013 Chiu Chi Ho. All rights reserved.
//

#import "EventViewController.h"
#import "PreferenceManager.h"


@interface EventViewController ()
{
	NSString *_currentGroup;
}
@property (nonatomic, retain) NSString *currentGroup;
@end

@implementation EventViewController
@synthesize tableView = _tableView;

@synthesize currentGroup = _currentGroup;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

		
		self.title = NSLocalizedString(@"Event", @"Event");
		self.tabBarItem.image = [UIImage imageNamed:@"first"];

		UIBarButtonItem *groupBtn = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Group", @"Group") style:UIBarButtonItemStyleBordered target:self action:@selector(groupBtnDidClick:)];
		self.navigationItem.rightBarButtonItem = groupBtn;
		[groupBtn release];
		
		UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshBtnDidClick:)];
		self.navigationItem.leftBarButtonItem = refreshBtn;
		[refreshBtn release];
	}
	
    return self;
}

- (void)dealloc
{
	[_tableView release];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	
	[super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:@"GroupIdDidChange" object:nil];
	
	//Leo Debug
	PreferenceManager *mgr = [PreferenceManager sharedManager];
	NSString *groupId = [mgr loadGroupId];
	
	if(groupId == nil)	//Should only happens in first launch, the group id is not ready
	{
		self.navigationItem.title = @"-";
	}
	else
	{
		self.currentGroup = groupId;
		self.navigationItem.title = _currentGroup;
	}
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NSNotificationCenter
- (void)didReceiveNotification:(NSNotification *)notification
{
	if([[notification name] isEqualToString:@"GroupIdDidChange"])
	{
		NSString *groupId = [[notification userInfo] objectForKey:@"Group_Id"];
		
		self.currentGroup = groupId;
		self.navigationItem.title = _currentGroup;
		[self.tableView reloadData];
	}
}

#pragma mark - Selector
- (void)groupBtnDidClick:(id)sender
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Group", @"Group")
															 delegate:self
													cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
											   destructiveButtonTitle:nil
													otherButtonTitles:@"A", @"B", @"C", @"D", @"E", nil];
	[actionSheet showFromTabBar:self.tabBarController.tabBar];
	[actionSheet release];
}

- (void)refreshBtnDidClick:(id)sender
{
	[[TimeslotManager sharedManager] reloadTimeslot];
	
	[self.tableView reloadData];
}


#pragma mark - UIActionSheetDelegate
// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if(buttonIndex == 5)	//Cancel
		return;
	
	if(buttonIndex == 0)
		self.currentGroup = @"A";
	else if(buttonIndex == 1)
		self.currentGroup = @"B";
	else if(buttonIndex == 2)
		self.currentGroup = @"C";
	else if(buttonIndex == 3)
		self.currentGroup = @"D";
	else if(buttonIndex == 4)
		self.currentGroup = @"E";
	
	self.navigationItem.title = _currentGroup;
	
	[self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	NSString *title = [[TimeslotManager sharedManager] getDateStringOnDay:section group:_currentGroup];
	return title;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	NSInteger numDay = [[TimeslotManager sharedManager] getNumberOfDay:_currentGroup];
	return numDay;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger numEvent = [[TimeslotManager sharedManager] getNumberOfEventOnDay:section group:_currentGroup];
	return numEvent;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
	
	TimeslotManager *mgr = [TimeslotManager sharedManager];
	TimeslotModel *model = [ mgr getTimeslotModelOnDay:indexPath.section eventIndex:indexPath.row group:_currentGroup];
	
	cell.textLabel.text = model.hour;
	cell.backgroundColor = [EventTypeHelper getBackgroundColorByEventType:model.type];
	cell.textLabel.textColor = [EventTypeHelper getTextColorByEventType:model.type];
	
	return cell;
}

#pragma mark - UITableViewDelegate

@end
